package com.prs.crm.action.crm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerTrade;
import com.prs.crm.service.crm.CustomerLevelService;
import com.prs.crm.service.sys.LogService;

public class CustomerLevelAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private CustomerLevel customerLevel;
	private CustomerLevelService customerLevelService;
	private PaginationSupport pagination;
	private Integer[] customerLevelIds;
	private Collection<TreeNode> nodes;
	private LogService logService;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	@Action("listCustomerLevel")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerLevel.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerLevelService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "loadCustomerLevelTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadCustomerLevelTree() {
		nodes = new ArrayList<TreeNode>();
		List<CustomerLevel> levels = this.getCustomerLevelService()
				.getAllCustomerLevel();
		for (CustomerLevel level : levels) {
			TreeNode node = new TreeNode();
			node.setId(level.getId().toString());
			node.setText(level.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action("selectCustomerLevel")
	public String selectCustomerLevel() {
		return this.execute();
	}

	@Action("editCustomerLevel")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveCustomerLevel", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerLevel"),
			@Result(name = "error", type = "chain", location = "editCustomerLevel") })
	public String save() {

		if (customerLevel.getName() == null
				|| "".equals(customerLevel.getName().trim())) {
			this.addActionError("等级不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(CustomerLevel.class, customerLevel
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getCustomerLevelService().save(customerLevel);
		return SUCCESS;
	}

	@Action(value = "removeCustomerLevel", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerLevel"),
			@Result(name = "error", type = "chain", location = "listCustomerLevel") })
	public String remove() {
		if (customerLevelIds == null) {
			this.addActionError("请选择要删除的客户级别");
			return ERROR;
		}
		for (Integer id : customerLevelIds) {
			CustomerLevel level = this.getCustomerLevelService().get(id);
			if (level.getCustomers().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getCustomerLevelService().batchRemove(customerLevelIds);
		return SUCCESS;
	}

	public CustomerLevel getCustomerLevel() {
		return customerLevel;
	}

	public void setCustomerLevel(CustomerLevel customerLevel) {
		this.customerLevel = customerLevel;
	}

	public CustomerLevelService getCustomerLevelService() {
		return customerLevelService;
	}

	public void setCustomerLevelService(
			CustomerLevelService customerLevelService) {
		this.customerLevelService = customerLevelService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getCustomerLevelIds() {
		return customerLevelIds;
	}

	public void setCustomerLevelIds(Integer[] customerLevelIds) {
		this.customerLevelIds = customerLevelIds;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	
}
